package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.Teacher;
import controller.TableModelDeletedTeacher;
import controller.TableModelTeacher;

@SuppressWarnings("serial")
public class PanelTeacherList extends JPanel {

	private static PanelTeacherList single;

	JTable TeacherList = new JTable(new TableModelTeacher());
	JScrollPane JSP = new JScrollPane(TeacherList);

	JTable deletedTeacherList = new JTable(new TableModelDeletedTeacher());
	JScrollPane JSPD = new JScrollPane(deletedTeacherList);

	private JButton addTB = new JButton("Nieuwe docent");
	private JButton detailTB = new JButton("Docent details");
	private JButton detailTBD = new JButton("Docent details");
	private JButton JBAllDeletedTeachers = new JButton("Verwijderde");
	private JButton JBAllActiveTeachers = new JButton("Alle docenten");

	private PanelTeacherList() {

		setBounds(0, 0, 860, 720);
		setLayout(null);
		TeacherList.setRowSelectionInterval(0, 0);
		JSP.setBounds(280, 100, 280, 500);
		JSPD.setBounds(280, 100, 280, 500);
		addTB.setBounds(1, 100, 140, 40);
		detailTB.setBounds(1, 150, 140, 40);
		detailTBD.setBounds(1, 150, 140, 40);
		JBAllDeletedTeachers.setBounds(600, 100, 140, 40);
		JBAllActiveTeachers.setBounds(600, 100, 140, 40);

		JBAllActiveTeachers.setVisible(false);
		JSPD.setVisible(false);
		detailTBD.setVisible(false);

		addTB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				PanelHolder mf = PanelHolder.getInstance();
				mf.remove(PanelTeacherList.getInstance());
				mf.add(PanelAddTeacher.newInstance());
				mf.repaint();
				mf.validate();

			}

		});

		

		JBAllDeletedTeachers.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JSPD.setVisible(true);
				JBAllActiveTeachers.setVisible(true);

				JSP.setVisible(false);
				JBAllDeletedTeachers.setVisible(false);
				
				detailTB.setVisible(false);
				detailTBD.setVisible(true);
				
			}
		});
		JBAllActiveTeachers.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JSP.setVisible(true);
				JBAllDeletedTeachers.setVisible(true);

				JSPD.setVisible(false);
				JBAllActiveTeachers.setVisible(false);
				
				detailTBD.setVisible(false);
				detailTB.setVisible(true);
				
				
			}
		});

		detailTB.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				Teacher t = ((TableModelTeacher) TeacherList.getModel())
						.getSelectedRowObject(TeacherList.getSelectedRow());

				PanelTeacherDetails ptd = new PanelTeacherDetails(t);
				ptd.updateTeacherDetail(t);

				ptd.getJLname();
				ptd.getJLcode();

				ptd.getJLname2().setText(t.getTeacher_name());
				ptd.getJLcode2().setText(t.getTeacher_code());

				ptd.getJTName().setText(t.getTeacher_name());
				ptd.getJTCode().setText(t.getTeacher_code());

				PanelHolder ph = PanelHolder.getInstance();
				ph.remove(PanelTeacherList.getInstance());
				ph.add(ptd);
				ph.repaint();
				ph.validate();

			}
		});
		
		detailTBD.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				Teacher t = ((TableModelDeletedTeacher) deletedTeacherList.getModel())
						.getSelectedRowObject(deletedTeacherList.getSelectedRow());

				PanelTeacherDetails ptd = new PanelTeacherDetails(t);
				ptd.updateTeacherDetail(t);

				ptd.getJLname();
				ptd.getJLcode();

				ptd.getJLname2().setText(t.getTeacher_name());
				ptd.getJLcode2().setText(t.getTeacher_code());

				ptd.getJTName().setText(t.getTeacher_name());
				ptd.getJTCode().setText(t.getTeacher_code());

				PanelHolder ph = PanelHolder.getInstance();
				ph.remove(PanelTeacherList.getInstance());
				ph.add(ptd);
				ph.repaint();
				ph.validate();

			}
		});

		add(JSP);
		add(JSPD);
		add(addTB);
		add(JBAllDeletedTeachers);
		add(JBAllActiveTeachers);
		add(detailTB);
		add(detailTBD);

	}

	public static PanelTeacherList getInstance() {
		if (single == null) {
			single = new PanelTeacherList();
		}
		return single;
	}

	public void selfDestroy() {
		single = null;

	}

	public JTable getTeacherList() {
		return TeacherList;
	}

	public void setTeacherList(JTable teacherList) {
		TeacherList = teacherList;
	}
}
	